package com.stevin.tool.utils;

import java.util.stream.Stream;

/**
 * @author mars
 * @date 2018/11/11
 */
public class CodeUtils {

    private final static String[] collectionKey = {"Set", "HashMap", "Map", "List"};

    public static boolean isCollection(String word) {
        return Stream.of(collectionKey)
                .anyMatch(type -> type.contentEquals(word));
    }

    public static boolean isNumeric(String str) {
        return str != null && str.matches("\\d*");
    }

    public static boolean isAnnotation(String word) {
        return word.startsWith("@");
    }

    private final static String[] nativeWordKey = {
            "public", "private", "class", "protected", "enum",
            "static", "final", "throws", "implements",
            "new", "null", "Exception", "value",
            "Throwable", "cause", "object", "hashCode",
            "extends", "main", "args"
    };


    public static boolean isNativeWord(String word) {
        return Stream.of(nativeWordKey)
                .anyMatch(symbol -> word.contains(symbol));
    }

    private final static String[] symbolsKey = {"{", "}", "(", ")", "<", ">", "=", ",", ".", "*", "\"", ";", "?", "%", "/", "-", "[", "]", "!"};

    public static String removeSymbols(String word) {
        for (String symbol : symbolsKey) {
            if (word.contains(symbol)) {
                word = word.replace(symbol, " ");
            }
        }
        return word;
    }

    public static boolean containSymbols(String word) {
        return Stream.of(symbolsKey)
                .anyMatch(symbol -> word.contains(symbol));
    }

    private final static String[] nativeTypeKey = {
            "String", "int", "Integer", "boolean", "void",
            "long", "Long", "T", "double", "Double", "R", "U"};


    public static boolean isNativeType(String word) {
        return Stream.of(nativeTypeKey)
                .anyMatch(type -> type.contentEquals(word));

    }

    public static boolean isPublicClassLine(String line) {
        line = line.trim();

        if (line.startsWith("public class")
                || line.startsWith("abstract class")
                || line.startsWith("public interface")) {
            return true;
        }
        return false;
    }

    public static boolean isMethodLine(String line) {
        line = line.trim();
        if (line.startsWith("public ")
                || line.startsWith("private ")
                || line.startsWith("protected ")) {
            return true;
        }
        return false;
    }
}
